<?php

/**
 * 全局设定
 * @author liuzilu <fotomxq@gmail.com>
 * @version 1
 * @package template
 */
//引用配置文件
require('..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'config.php');

//引用核心处理库
require(DIR_LIB . DS . 'core-file.php');
require(DIR_LIB . DS . 'core-header.php');
require(DIR_LIB . DS . 'core-error.php');
require(DIR_LIB . DS . 'core-log.php');
require(DIR_LIB . DS . 'core-db.php');
require(DIR_LIB . DS . 'core-cache.php');
require(DIR_LIB . DS . 'core-filter.php');
require(DIR_LIB . DS . 'core-ip.php');

//引用必要插件
require(DIR_LIB . DS . 'plug-substrutf8.php');

//引用必要库
require(DIR_LIB . DS . 'sys-config.php');
require(DIR_LIB . DS . 'sys-user.php');

//构建缓冲对象
$cache = new CoreCache(CACHE_ON, CACHE_LIMIT_TIME, CACHE_DIR);

//尝试连接数据库
$db = new CoreDB($dbDSN, $dbUser, $dbPasswd, $dbPersistent, $dbEncoding);
if (!$db) {
    die('Cannot connect DB.');
}

//构建过滤器
$filter = new CoreFilter();

//构建IP对象
$ip = new CoreIP();
$ipAddr = $ip->getIP();

//构建日志对象
$log = new CoreLog(LOG_ON, LOG_DIR, LOG_TYPE, $ipAddr);

//构建系统配置对象
$config = new SysConfig($db, $tables['config']['name']);

//获取网站页面通用数据
$cacheWebDataName = 'WEB-PAGE-DATA';
$webData = $cache->get($cacheWebDataName);
if ($webData) {
    $webData = json_decode($webData, true);
} else {
    $webData = null;
    $webData['WEB-TITLE'] = $config->get(1);
    $webData['USER-LIMIT-TIME'] = $config->get('USER-LIMIT-TIME');
    $cache->set($cacheWebDataName, json_encode($webData));
}

//构建用户对象
$user = new SysUser($db, $tables['user']['name'], $tables['user-meta']['name'], USER_SESSION_LOGIN_NAME, (int) $webData['USER-LIMIT-TIME']);
$user->fieldsUser = $tables['user']['fields'];
$user->fieldsMeta = $tables['user-meta']['fields'];
?>